home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / viewers / mpgplyr1.lha / src / decoders.h < prev    next >
C/C++ Source or Header  |  1992-12-08  |  13KB  |  296 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. /*
  22.  * decoders.h
  23.  *
  24.  * This file contains the declarations of structures required for Huffman
  25.  * decoding
  26.  *
  27.  */
  28.  
  29. /* Include util.h for bit i/o parsing macros. */
  30.  
  31. #include "util.h"
  32.  
  33. /* Code for unbound values in decoding tables */
  34. #define ERROR -1
  35. #define DCT_ERROR 63
  36.  
  37. #define MACRO_BLOCK_STUFFING 34
  38. #define MACRO_BLOCK_ESCAPE 35
  39.  
  40. /* Two types of DCT Coefficients */
  41. #define DCT_COEFF_FIRST 0
  42. #define DCT_COEFF_NEXT 1
  43.  
  44. /* Special values for DCT Coefficients */
  45. #define END_OF_BLOCK 62
  46. #define ESCAPE 61
  47.  
  48. /* Structure for an entry in the decoding table of 
  49.  * macroblock_address_increment */
  50. typedef struct {
  51.   unsigned int value;       /* value for macroblock_address_increment */
  52.   int num_bits;             /* length of the Huffman code */
  53. } mb_addr_inc_entry;
  54.  
  55. /* Decoding table for macroblock_address_increment */
  56. mb_addr_inc_entry mb_addr_inc[2048];
  57.  
  58.  
  59. /* Structure for an entry in the decoding table of macroblock_type */
  60. typedef struct {
  61.   unsigned int mb_quant;              /* macroblock_quant */
  62.   unsigned int mb_motion_forward;     /* macroblock_motion_forward */
  63.   unsigned int mb_motion_backward;    /* macroblock_motion_backward */
  64.   unsigned int mb_pattern;            /* macroblock_pattern */
  65.   unsigned int mb_intra;              /* macroblock_intra */
  66.   int num_bits;                       /* length of the Huffman code */
  67. } mb_type_entry;
  68.  
  69. /* Decoding table for macroblock_type in predictive-coded pictures */
  70. mb_type_entry mb_type_P[64];
  71.  
  72. /* Decoding table for macroblock_type in bidirectionally-coded pictures */
  73. mb_type_entry mb_type_B[64];
  74.  
  75.  
  76. /* Structures for an entry in the decoding table of coded_block_pattern */
  77. typedef struct {
  78.   unsigned int cbp;            /* coded_block_pattern */
  79.   int num_bits;                /* length of the Huffman code */
  80. } coded_block_pattern_entry;
  81.  
  82. /* External declaration of coded block pattern table. */
  83.  
  84. extern coded_block_pattern_entry coded_block_pattern[512];
  85.  
  86.  
  87.  
  88. /* Structure for an entry in the decoding table of motion vectors */
  89. typedef struct {
  90.   int code;              /* value for motion_horizontal_forward_code,
  91.               * motion_vertical_forward_code, 
  92.               * motion_horizontal_backward_code, or
  93.               * motion_vertical_backward_code.
  94.               */
  95.   int num_bits;          /* length of the Huffman code */
  96. } motion_vectors_entry;
  97.  
  98.  
  99. /* Decoding table for motion vectors */
  100. motion_vectors_entry motion_vectors[2048];
  101.  
  102.  
  103. /* Structure for an entry in the decoding table of dct_dc_size */
  104. typedef struct {
  105.   unsigned int value;    /* value of dct_dc_size (luminance or chrominance) */
  106.   int num_bits;          /* length of the Huffman code */
  107. } dct_dc_size_entry;
  108.  
  109. /* External declaration of dct dc size lumiance table. */
  110.  
  111. extern dct_dc_size_entry dct_dc_size_luminance[128];
  112.  
  113. /* External declaration of dct dc size chrom table. */
  114.  
  115. extern dct_dc_size_entry dct_dc_size_chrominance[256];
  116.  
  117.  
  118. /* DCT coeff tables. */
  119.  
  120. #define RUN_MASK 0xfc00
  121. #define LEVEL_MASK 0x03f0
  122. #define NUM_MASK 0x000f
  123. #define RUN_SHIFT 10
  124. #define LEVEL_SHIFT 4
  125.  
  126. /* External declaration of dct coeff tables. */
  127.  
  128. extern unsigned short int dct_coeff_tbl_0[256];
  129. extern unsigned short int dct_coeff_tbl_1[16];
  130. extern unsigned short int dct_coeff_tbl_2[4];
  131. extern unsigned short int dct_coeff_tbl_3[4];
  132. extern unsigned short int dct_coeff_next[256];
  133. extern unsigned short int dct_coeff_first[256];
  134.  
  135. #define DecodeDCTDCSizeLum(macro_val)                    \
  136. {                                                    \
  137.   unsigned int index;                                \
  138.                                                      \
  139.   show_bits7(&index);                              \
  140.                                                      \
  141.   *(macro_val) = dct_dc_size_luminance[index].value;       \
  142.                                                      \
  143.   flush_bits(dct_dc_size_luminance[index].num_bits); \
  144. }
  145.  
  146. #define DecodeDCTDCSizeChrom(macro_val)                      \
  147. {                                                        \
  148.   unsigned int index;                                    \
  149.                                                          \
  150.   show_bits8(&index);                                  \
  151.                                                          \
  152.   *(macro_val) = dct_dc_size_chrominance[index].value;         \
  153.                                                          \
  154.   flush_bits(dct_dc_size_chrominance[index].num_bits);   \
  155. }
  156.  
  157. #define DecodeDCTCoeff(type, run, level)                                   \
  158. {                                                                          \
  159.   unsigned int temp_run, index, temp_level, num_bits;                   \
  160.   unsigned short int *dct_coeff_tbl;                                       \
  161.   unsigned int value;                                     \
  162.                                                                            \
  163.   show_bits8(&index);                                                    \
  164.   if (type == DCT_COEFF_FIRST)                                             \
  165.     dct_coeff_tbl = dct_coeff_first;                                       \
  166.   else                                                                     \
  167.     dct_coeff_tbl = dct_coeff_next;                                        \
  168.                                                                            \
  169.   if (index > 3) {                                                         \
  170.     value = dct_coeff_tbl[index];                                     \
  171.     temp_run = (value & RUN_MASK) >> RUN_SHIFT;             \
  172.     if (temp_run == END_OF_BLOCK) {                                        \
  173.       *run = END_OF_BLOCK;                                                 \
  174.       *level = END_OF_BLOCK;                                               \
  175.     }                                                                      \
  176.     else {                                                                 \
  177.       num_bits = (value & NUM_MASK) + 1;                      \
  178.       flush_bits(num_bits);                                                \
  179.       if (temp_run != ESCAPE) {                                            \
  180.      value = dct_coeff_tbl[index];                                     \
  181.          temp_level = (value & LEVEL_MASK) >> LEVEL_SHIFT;                 \
  182.          *run = temp_run;                                                  \
  183.          get_bits1(&value);                                                    \
  184.          *level = value ? -temp_level : temp_level;                           \
  185.        }                                                                   \
  186.        else {    /* temp_run == ESCAPE */                                  \
  187.          get_bits6(run);                                                   \
  188.          get_bits8(&temp_level);                                           \
  189.          if (temp_level != 0 && temp_level != 128)                         \
  190.         *level = ((int) (temp_level << 24)) >> 24;                     \
  191.          else if (temp_level == 0) {                                       \
  192.             get_bits8(&temp_level);                                        \
  193.                *level = temp_level;                                           \
  194.          assert(*level >= 128);                                         \
  195.          }                                                                 \
  196.          else {                                                            \
  197.             get_bits8(&temp_level);                                        \
  198.               *level = temp_level - 256;                                     \
  199.         assert(*level <= -128 && *level >= -255);                      \
  200.          }                                                                 \
  201.        }                                                                   \
  202.     }                                                                      \
  203.   }                                                                        \
  204.   else {  \
  205.     if (index == 2) {                                                   \
  206.       show_bits10(&index);                                                 \
  207.       value = dct_coeff_tbl_2[index & 3];                                     \
  208.     }                                                                        \
  209.     else if (index == 3) {                                                   \
  210.       show_bits10(&index);                                                 \
  211.       value = dct_coeff_tbl_3[index & 3];                                     \
  212.     }                                                                        \
  213.     else if (index == 1) {                                                   \
  214.       show_bits12(&index);                                                 \
  215.       value = dct_coeff_tbl_1[index & 15];                                     \
  216.     }                                                                        \
  217.     else {   /* index == 0 */                                                \
  218.       show_bits16(&index);                                                 \
  219.       value = dct_coeff_tbl_0[index & 255];                                     \
  220.     }                                                                        \
  221.     *run = (value & RUN_MASK) >> RUN_SHIFT;               \
  222.     temp_level = (value & LEVEL_MASK) >> LEVEL_SHIFT;     \
  223.     num_bits = (value & NUM_MASK) + 1;                    \
  224.     flush_bits(num_bits);                                                  \
  225.     get_bits1(&value);                                                         \
  226.     *level = value ? -temp_level : temp_level;                                 \
  227.   }  \
  228. }
  229.  
  230. #define DecodeDCTCoeffFirst(runval, levelval)         \
  231. {                                                     \
  232.   DecodeDCTCoeff(DCT_COEFF_FIRST, runval, levelval);  \
  233. }          
  234.  
  235. #define DecodeDCTCoeffNext(runval, levelval)          \
  236. {                                                     \
  237.   DecodeDCTCoeff(DCT_COEFF_NEXT, runval, levelval);   \
  238. }
  239.  
  240. /*
  241.  *--------------------------------------------------------------
  242.  *
  243.  * DecodeMBAddrInc --
  244.  *
  245.  *      Huffman Decoder for macro_block_address_increment; the location
  246.  *      in which the result will be placed is being passed as argument.
  247.  *      The decoded value is obtained by doing a table lookup on
  248.  *      mb_addr_inc.
  249.  *
  250.  * Results:
  251.  *      The decoded value for macro_block_address_increment or ERROR
  252.  *      for unbound values will be placed in the location specified.
  253.  *
  254.  * Side effects:
  255.  *      Bit stream is irreversibly parsed.
  256.  *
  257.  *--------------------------------------------------------------
  258.  */
  259. #define DecodeMBAddrInc(ptr)                \
  260. {                            \
  261.     unsigned int index;                    \
  262.     show_bits11(&index);                \
  263.     *(ptr) = mb_addr_inc[index].value;            \
  264.     flush_bits(mb_addr_inc[index].num_bits);        \
  265. }
  266.  
  267. /*
  268.  *--------------------------------------------------------------
  269.  *
  270.  * DecodeMotionVectors --
  271.  *
  272.  *      Huffman Decoder for the various motion vectors, including
  273.  *      motion_horizontal_forward_code, motion_vertical_forward_code,
  274.  *      motion_horizontal_backward_code, motion_vertical_backward_code.
  275.  *      Location where the decoded result will be placed is being passed
  276.  *      as argument. The decoded values are obtained by doing a table
  277.  *      lookup on motion_vectors.
  278.  *
  279.  * Results:
  280.  *      The decoded value for the motion vector or ERROR for unbound
  281.  *      values will be placed in the location specified.
  282.  *
  283.  * Side effects:
  284.  *      Bit stream is irreversibly parsed.
  285.  *
  286.  *--------------------------------------------------------------
  287.  */
  288.  
  289. #define DecodeMotionVectors(ptr)            \
  290. {                            \
  291.   unsigned int index;                    \
  292.   show_bits11(&index);                    \
  293.   *(ptr) = motion_vectors[index].code;            \
  294.   flush_bits(motion_vectors[index].num_bits);        \
  295. }
  296.